package com.espirit.builder.biz;

import org.springframework.stereotype.Component;

import com.espirit.common.WFMessage;
import com.espirit.eap.pagelayout.DetailLayout;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.FunctionButton;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.eap.pagelayout.PageReference;
import com.espirit.eap.pagelayout.SectionElement;

@Component
public class WFMessageBuilder extends MessageBuilder {
	static final String FIELD_STATUSTYPE="statusType";
	static final String FIELD_STATUSFLAG="statusFlag";
	
	protected FunctionButton accept = null;
	
	@Override
	public boolean init() {
		return super.setClass(WFMessage.class, "ToDo", "待办事宜") ;
	}
	
	@Override
	public void buildFields() {
		addModelField(FIELD_WFSTATUS, "Decide", "决定");
		addModelField(FIELD_STATUSTYPE, "Status", "状态");
		addModelField(FIELD_STATUSFLAG, "Status", "状态");
		super.buildFields();
	}
	
	@Override
	public void prepareLayoutElements() {
		accept = createFunctionButton("_accept", "accept", "审批",this.clazz,"_accept",false,null,null);
		ModelField mfPhase = getModelField(FIELD_PHASE);
		ModelField mfPhaseDescription = getModelField(FIELD_PHASEDESCRIPTION);
		
		ModelField mfTemplate = getModelField(FIELD_TEMPLATE);
		ModelField mfCreatedOn = getModelField(FIELD_CREATEDON);
		ModelField mfStatus = getModelField(FIELD_WFSTATUS);
		ModelField mfDescription = getModelField(FIELD_DESCRIPTION);
		ModelField mfInstance = getModelField(FIELD_INSTANCE);
		
		PageReference eventReference = new PageReference();
		eventReference.setUrl("/pages/espirit/common/wfmessageHeader.ftl");

		ModelField mfFrom = getModelField(FIELD_FROM);
		ModelField mfTitle = getModelField(FIELD_TITLE);
		addSearchField(getModelField(FIELD_STATUSTYPE));
		addListField(getModelField(FIELD_STATUSFLAG) ,mfFrom,mfTitle,mfPhase,mfCreatedOn,mfDescription);
		detailElements.add(eventReference);
		addDetailField(mfInstance,mfTemplate,mfPhase,mfCreatedOn,mfPhaseDescription ,mfStatus,mfDescription);
	}
	
	@Override
	protected void afterBuilderLayout() {
		bindButtonToLayout(accept, model.getDefaultDetailLayout());
		
		DetailLayout bs = model.getDefaultDetailLayout();
		bs.findSectionElement(FIELD_PHASEDESCRIPTION).setTagName("textarea");
		bs.findSectionElement(FIELD_INSTANCE + ".id").setVisible(false);
		bs.findSectionElement(FIELD_WFSTATUS + ".id").setRequired(true);
		
		FlatSection buttonSection = (FlatSection)model.getDefaultListLayout().getChildren().get(0);
		SectionElement se =  buttonSection.getSectionElement(FIELD_STATUSTYPE);
		se.addExProperty("collection", "fn.service('" + modelService.getModelName(WFMessage.class) + "').statusType");
		se.setTagName("select");
		updateObject(model);
		
		
		super.afterBuilderLayout();
	}
	
	@Override
	protected void setDefaultLayoutButtons() {
//		addExtraButton(accept);
	}
	
	
}
